package com.wing.storage.Controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.PageResult;
import com.wing.storage.model.entity.ProductStorageWorkOutDetail;
import com.wing.storage.model.response.PurchaseOutDetailInfoExportVO;
import com.wing.storage.service.ProductStorageWorkOutService;
import com.wing.utils.ExcelUtils;
import com.wing.web.db.config.util.DynamicDataSource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 仓库产品出库作业详情管理-Controller
 *
 */
@Validated
@Api(value = "仓库产品出库作业详情管理", tags = "#/storage/productStorageWorkOutDetail")
@RestController("ProductStorageWorkOutDetailController")
@RequestMapping("storage/admin/productStorageWorkOutDetail")
@RequiredArgsConstructor
public class ProductStorageWorkOutDetailController {

    @Autowired
    private ProductStorageWorkOutService productStorageWorkOutService;

    @ApiOperation(value = "查询出库作业详情列表", notes = "搜索")
    @GetMapping("/list")
    public PageResult<ProductStorageWorkOutDetail> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(value = "skuName", required = false) String skuName,
            @RequestParam(value = "skuCode", required = false) String skuCode,
            @RequestParam(value = "workNo", required = false) String workNo,
            @RequestParam(value = "warehouseShelfSpaceCode", required = false) String warehouseShelfSpaceCode,
            @RequestParam(value = "warehouseId", required = false) String warehouseId
      ) {
        Page<ProductStorageWorkOutDetail> pg = new Page<>(page, limit);
        Map<String, Object> map = new HashMap();
        map.put("warehouseId", warehouseId);
        map.put("warehouseShelfSpaceCode", warehouseShelfSpaceCode);
        map.put("skuName", skuName);
        map.put("workNo", workNo);
        map.put("skuCode", skuCode);
        String databaseUser = DynamicDataSource.USER_PREFIX;
        if(StringUtils.isNotBlank(DynamicDataSource.name.get())){
            databaseUser = databaseUser + "-" + DynamicDataSource.name.get();
        }
        map.put("databaseUser", databaseUser);
        IPage<ProductStorageWorkOutDetail> list = productStorageWorkOutService.getProductStorageWorkOutDetail(pg, map);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }




    @ApiOperation(value = "导出")
    @GetMapping("/export")
    public void export(HttpServletResponse response,
                       @RequestParam(name = "skuCode", required = false) String skuCode,
                       @RequestParam(name = "skuName", required = false) String skuName,
                       @RequestParam(name = "skuSpec", required = false) String skuSpec,
                       @RequestParam(name = "warehouseId", required = false) String warehouseId
    ){
        List<PurchaseOutDetailInfoExportVO> list = new ArrayList<>();
        Map params = new HashMap();
        if(StringUtils.isNotBlank(skuCode)){
            params.put("skuCode", skuCode);
        }
        if(StringUtils.isNotBlank(skuName)){
            params.put("skuName", skuName);
        }

        if(StringUtils.isNotBlank(skuSpec)){
            params.put("skuSpec", skuSpec);
        }

        if (StringUtils.isNotBlank(warehouseId)) {
            params.put("warehouseId", warehouseId);
        }
        String databaseUser = DynamicDataSource.USER_PREFIX;
        if (StringUtils.isNotBlank(DynamicDataSource.name.get())) {
            databaseUser = databaseUser + "-" + DynamicDataSource.name.get();
        }
        params.put("databaseUser", databaseUser);

        list = productStorageWorkOutService.exportProductStorageWorkOutDetail(params);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
        String timestamp = sdf.format(new Date());
        // 生成带时间戳的文件名
        String fileName = "出库详细表_" + timestamp;
        ExcelUtils.export(response,fileName,list, PurchaseOutDetailInfoExportVO.class);
    }


}
